/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2023-2023. All rights reserved.
 */

package com.huawei.api.builder;

import com.alibaba.fastjson.JSONArray;
import com.huawei.api.bean.Rule;
import com.huawei.api.exception.BaseCustomerException;
import com.huawei.api.utils.FileUtil;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * 规则配置加载
 */
@Data
public class RuleCfgBuilder implements Builder {
    private static final Logger LOGGER = LoggerFactory.getLogger(RuleCfgBuilder.class);

    private final List<Rule> defaultRules;

    private final List<Rule> customeRules;

    private final String ruleCfgPath;

    public RuleCfgBuilder(String ruleCfgPath) {
        this.ruleCfgPath = ruleCfgPath;
        this.defaultRules = new ArrayList<>();
        this.customeRules = new ArrayList<>();
    }

    @Override
    public void build() throws BaseCustomerException {
        LOGGER.info("-----------------RuleCfgBuilder start build---------------------------");
        boolean isExists = FileUtil.fileIsExists(ruleCfgPath);
        // 没有配置规则文件则读取，默认的规则文件
        if (isExists) {
            loadCustomConfigRule();
        }
        loadDefaultConfigRule();
    }

    /**
     * 加载默认规则
     */
    private void loadDefaultConfigRule() throws BaseCustomerException {
        // 读取默认规则文件内容，json转化为对象
        InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("rule.json");
        if (inputStream == null) {
            LOGGER.info("<<< RuleCfgBuilder loadDefaultConfigRule inputStream is null");
            return;
        }
        try {
            String content = FileUtil.getResourceFileContent(inputStream);
            LOGGER.info("<<< RuleCfgBuilder loadDefaultConfigRule content ={} ", content);
            if (StringUtils.isBlank(content)){
                return;
            }
            JSONArray jsonArray = JSONArray.parseArray(content.toString());
            List<Rule> defaultRuleList = jsonArray.toJavaList(Rule.class);
            defaultRules.addAll(defaultRuleList);
        } catch (BaseCustomerException exception) {
            LOGGER.debug("some error occur in default rule config builder");
            throw new BaseCustomerException("load default rule config error");
        }
    }

    /**
     * 加载用户配置的rule
     *
     * @throws BaseCustomerException 异常信息
     */
    private void loadCustomConfigRule() throws BaseCustomerException {
        try {
            String content = FileUtil.getFileContent(ruleCfgPath);
            LOGGER.info("<<< RuleCfgBuilder loadCustomConfigRule content ={} ", content);
            if (StringUtils.isBlank(content)){
                return;
            }
            JSONArray jsonArray = JSONArray.parseArray(content.toString());
            List<Rule> languageList = jsonArray.toJavaList(Rule.class);
            customeRules.addAll(languageList);
        } catch (BaseCustomerException exception) {
            LOGGER.debug("some error occur in custom rule config builder");
            throw new BaseCustomerException("load custom rule config error");
        }
    }
}
